package flowlight.action;

import flowlight.entity.User;
import flowlight.service.UserService;
import flowlight.utils.JsonVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller("userController")
@RequestMapping("flowlight")
public class UserAction {

    @Resource(name = "UserService")
    public UserService service;

    @RequestMapping("getUser")
    @ResponseBody
    public User findById(Integer userId) {
        User ser = service.findById(userId);
        System.out.print(ser.getUserName());
        return ser;
    }

    @RequestMapping("login")
    @ResponseBody
    public JsonVo<User> login(String phone,String pswd) {
        User user = service.findByPhone(phone);
        JsonVo<User> jsonVo = new JsonVo<User>();
        if (user == null) {
            jsonVo.setCode(201);
            jsonVo.setMsg("用户未注册");
        } else {
            if (user.getPassword().equals(pswd)) {
                jsonVo.setCode(200);
                jsonVo.setMsg("登录成功");
                jsonVo.setData(user);
            } else {
                jsonVo.setCode(201);
                jsonVo.setMsg("密码错误");
            }
        }
        return jsonVo;
    }

    @RequestMapping("register")
    @ResponseBody
    public JsonVo<String> register(User user) {
        JsonVo<String> jsonVo = new JsonVo<String>();
        if (user != null) {
            service.insert(user);
            jsonVo.setCode(200);
            jsonVo.setMsg("注册成功");
        }
        return jsonVo;
    }
}
